local jianying = fk.CreateSkill {
  name = "hx__jianying_gu",
}


jianying:addEffect("viewas", {
  name = "hx__jianying_gu",
  times = function(self, player)
    return player.phase == Player.Play and 5 - player:usedEffectTimes(self.name, Player.HistoryPhase) or -1
  end,
  prompt = "#hx__jianying_gu-active",
  interaction = function(self, player)
    local all_names = Fk:getAllCardNames("btd")
    local names = player:getViewAsCardNames(jianying.name, all_names)
    if #names == 0 then return end
    return UI.CardNameBox {choices = names, all_choices = all_names}
  end,
  handly_pile = true,
  card_filter = function(self, player, to_select, selected)
    return #selected == 0
  end,
  view_as = function(self, player, cards)
    if #cards ~= 1 or not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcards(cards)

    local suitstrings = {"spade", "heart", "club", "diamond"}
    local suits = {Card.Spade, Card.Heart, Card.Club, Card.Diamond}
    local colors = {Card.Black, Card.Red, Card.Black, Card.Diamond}
    local suit = player:getMark("hx__jianying_gu_suit-turn")
    if table.contains(suitstrings, suit) then
      card.suit = suits[table.indexOf(suitstrings, suit)]
      card.color = colors[table.indexOf(suitstrings, suit)]
    end

    card.skillName = jianying.name
    return card
  end,
  after_use = function (self, player, use)
    player.room:addPlayerMark(player, "@hx__jianying_gu_count-turn", 1)
  end,
  enabled_at_play = function(self, player)
    return player:usedEffectTimes(self.name,Player.HistoryPhase) < 5
  end,
  enabled_at_response = function(self, player, response)
    return player:usedEffectTimes(self.name,Player.HistoryPhase) < 5
  end,
})

jianying:addEffect(fk.CardUsing, {
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(jianying.name) then
      local use_event = player.room.logic:getEventsByRule(GameEvent.UseCard, 1, function (e)
        if e.id < player.room.logic:getCurrentEvent().id then
          return e.data.from == player
        end
      end, 1)
      if #use_event == 1 then
        local use = use_event[1].data
        return (use.card.number == data.card.number and data.card.number > 0) or use.card:compareSuitWith(data.card)
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local count = math.max(player:getMark("@hx__jianying_gu_count-turn"), 1)
    player:drawCards(count, jianying.name)
    player.room:addPlayerMark(player, "@hx__jianying_gu_count-turn", 1)
  end,
})

jianying:addEffect(fk.AfterCardUseDeclared, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasSkill(jianying.name, true)
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "@hx__jianying_gu-turn", {data.card:getSuitString(true), data.card:getNumberStr()})
    if data.card.suit == Card.NoSuit then
      player.room:setPlayerMark(player, "hx__jianying_gu_suit-turn", 0)
    else
      player.room:setPlayerMark(player, "hx__jianying_gu_suit-turn", data.card:getSuitString())
    end
  end,
})

jianying:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@hx__jianying_gu-turn", 0)
  player.room:setPlayerMark(player, "hx__jianying_gu_suit-turn", 0)
end)

return jianying
